import java.awt.*;
import java.awt.event.KeyEvent;
import javax.swing.Timer;
/**
 * InputManagerTest tests the InputManager with a simple run-and-jump mechanism. The player moves and jumps using the arrow keys and the space bar. <p>Also, InputManagerTest demonstrates pausing a game by not updating the game elements if the game is paused.
 */
public class InputManagerTest extends GameCore {
    public static void main(String[] args) {
        new InputManagerTest().run();
    }
    /**
	 * @uml.property  name="exit"
	 * @uml.associationEnd  
	 */
    protected GameAction exit;
    /**
	 * @uml.property  name="jump"
	 * @uml.associationEnd  
	 */
    protected GameAction jump;
    /**
	 * @uml.property  name="newgame"
	 * @uml.associationEnd  
	 */
    protected GameAction newgame;
    /**
	 * @uml.property  name="sp1"
	 * @uml.associationEnd  
	 */
    protected GameAction sp1;
    /**
	 * @uml.property  name="sp2"
	 * @uml.associationEnd  
	 */
    protected GameAction sp2;
    /**
	 * @uml.property  name="sp3"
	 * @uml.associationEnd  
	 */
    protected GameAction sp3;
    /**
	 * @uml.property  name="sp4"
	 * @uml.associationEnd  
	 */
    protected GameAction sp4;
    /**
	 * @uml.property  name="sp5"
	 * @uml.associationEnd  
	 */
    protected GameAction sp5;
    /**
	 * @uml.property  name="sp6"
	 * @uml.associationEnd  
	 */
    protected GameAction sp6;
    /**
	 * @uml.property  name="sp7"
	 * @uml.associationEnd  
	 */
    protected GameAction sp7;
    /**
	 * @uml.property  name="sp8"
	 * @uml.associationEnd  
	 */
    protected GameAction sp8;
    
    /**
	 * @uml.property  name="moveLeft"
	 * @uml.associationEnd  
	 */
    protected GameAction moveLeft;
    /**
	 * @uml.property  name="moveright"
	 * @uml.associationEnd  
	 */
    protected GameAction moveright;
    
    /**
	 * @uml.property  name="jump0"
	 * @uml.associationEnd  
	 */
    protected GameAction jump0;
    /**
	 * @uml.property  name="moveLeft0"
	 * @uml.associationEnd  
	 */
    protected GameAction moveLeft0;
    /**
	 * @uml.property  name="moveright0"
	 * @uml.associationEnd  
	 */
    protected GameAction moveright0;
    
    /**
	 * @uml.property  name="pause"
	 * @uml.associationEnd  
	 */
    protected GameAction pause;
    /**
	 * @uml.property  name="inputManager"
	 * @uml.associationEnd  
	 */
    protected InputManager inputManager;
    
    private String[] winners={"","","","","","","","",""};
    /**
	 * @uml.property  name="player"
	 * @uml.associationEnd  
	 */
    private Player player;
	/**
	 * @uml.property  name="player0"
	 * @uml.associationEnd  
	 */
	private Player player0;
	/**
	 * @uml.property  name="player1"
	 * @uml.associationEnd  
	 */
	private Player player1;
	/**
	 * @uml.property  name="player2"
	 * @uml.associationEnd  
	 */
	private Player player2;
	/**
	 * @uml.property  name="player3"
	 * @uml.associationEnd  
	 */
	private Player player3;
	/**
	 * @uml.property  name="player4"
	 * @uml.associationEnd  
	 */
	private Player player4;
	/**
	 * @uml.property  name="player5"
	 * @uml.associationEnd  
	 */
	private Player player5;
	/**
	 * @uml.property  name="player6"
	 * @uml.associationEnd  
	 */
	private Player player6;
    private String[] pnames={"Mario","Megaman","Samus","Goku","Blinky","pla6","pla7","pla8"};
    
    private Timer timer = new Timer(1000, null);
    private long startTime;
    private String clock="00:00";
       		
    private Image bgImage;
	private Image bgMenu;
    /**
	 * @uml.property  name="paused"
	 */
    private boolean paused;
    private String splayer="";
    
    public void init() {
        
    	super.init();
		
    	Window window = screen.getFullScreenWindow();
        inputManager = new InputManager(window);
        paused = true;
        createGameActions();
        createSprite(); 
           
    }
    /**
	 * Tests whether the game is paused or not.
	 * @uml.property  name="paused"
	 */
    public boolean isPaused() {
        return paused;
    }
    /**
	 * Sets the paused state.
	 * @uml.property  name="paused"
	 */
    public void setPaused(boolean p) {
        if (paused != p) {
            this.paused = p;
            inputManager.resetAllGameActions();
        }
    }
    
    public void update(long elapsedTime) {
        // check input that can happen whether paused or not
        checkSystemInput();
        if (!paused) {
            // check game input
            checkGameInput();
            
          //check bounds player 1
	    	if (player.getX()<0)
	    	{
	    		player.setVelocityX(Math.abs(player.getVelocityX()));
	    	}
	    	else if (player.getX() + player.getWidth()>= screen.getWidth()+20)
	    	{
	    		player.setVelocityX(0);
	    		player.setX(screen.getWidth()-20);
	    		win(pnames[0]);
	    		
	    		
	       	}
	    	
	    	//check bounds player 2
	    	if (player0.getX()<0)
	    	{
	    		player0.setVelocityX(Math.abs(player0.getVelocityX()));
	    	}
	    	else if (player0.getX() + player0.getWidth()>= screen.getWidth()+20)
	    	{
	    		player0.setVelocityX(0);
	    		player0.setX(screen.getWidth()-20);
	    		win(pnames[1]);
	    		
	    	}

	    	//check bounds player 3
		    	if (player1.getX()<0)
		    	{
		    		player1.setVelocityX(Math.abs(player1.getVelocityX()));
		    	}
		    	else if (player1.getX() + player1.getWidth()>= screen.getWidth()+20)
		    	{
		    		player1.setVelocityX(0);
		    		player1.setX(screen.getWidth()-20);
		    		win(pnames[2]);
		    				    	
		    	}
		    	
          //check bounds player 4
		    	if (player2.getX()<0)
		    	{
		    		player2.setVelocityX(Math.abs(player2.getVelocityX()));
		    	}
		    	else if (player2.getX() + player2.getWidth()>= screen.getWidth()+20)
		    	{
		    		player2.setVelocityX(0);
		    		player2.setX(screen.getWidth()-20);
		    		win(pnames[3]);		
		    		
		    	}
		          //check bounds player 1
		    	if (player3.getX()<0)
		    	{
		    		player3.setVelocityX(Math.abs(player3.getVelocityX()));
		    	}
		    	else if (player3.getX() + player3.getWidth()>= screen.getWidth()+20)
		    	{
		    		player3.setVelocityX(0);
		    		player3.setX(screen.getWidth()-20);
		    		win(pnames[4]);
		    		
		       	}
		    	
	          //check bounds player 6
		    	if (player4.getX()<0)
		    	{
		    		player4.setVelocityX(Math.abs(player4.getVelocityX()));
		    	}
		    	else if (player4.getX() + player4.getWidth()>= screen.getWidth()+20)
		    	{	
		    		player4.setVelocityX(0);
		    		player4.setX(screen.getWidth()-20);
		    		win(pnames[5]);
		    		
		    	}

		    	//check bounds player7
		    	if (player5.getX()<0)
		    	{
		    		player5.setVelocityX(Math.abs(player5.getVelocityX()));
		    	}
		    	else if (player5.getX() + player5.getWidth()>= screen.getWidth()+20)
		    	{
		    		//player5.setVelocityX(-Math.abs(player5.getVelocityX()));
		    		player5.setVelocityX(0);
		    		player5.setX(screen.getWidth()-20);
		    		win(pnames[6]);
		    				    		
		    	}
			    	
	          //check bounds player 8
		    	if (player6.getX()<0)
		    	{
		    		player6.setVelocityX(Math.abs(player6.getVelocityX()));
		    	}
		    	else if (player6.getX() + player6.getWidth()>= screen.getWidth()+20)
		    	{
		    		//player6.setVelocityX(-Math.abs(player6.getVelocityX()));
		    		player6.setVelocityX(0);
		    		player6.setX(screen.getWidth()-20);
		    		win(pnames[7]);
		    				    					    		
		    	}
	   
		    	
		        // update sprite
		        player.update(elapsedTime);
		        player0.update(elapsedTime);
		        player1.update(elapsedTime);
		        player2.update(elapsedTime);
		        player3.update(elapsedTime);
		        player4.update(elapsedTime);
		        player5.update(elapsedTime);
		        player6.update(elapsedTime);
		        
            
            
            //race clock
            int currentTime=(int)(System.currentTimeMillis()-startTime);
            
            java.text.DecimalFormat nft = new
            java.text.DecimalFormat("#00000.###");

            clock = nft.format(currentTime);
            clock = clock.substring(0, 2) + ":" + clock.substring(2, 4);
            
        }
        
                
    }
    /**
        Checks input from GameActions that can be pressed
        regardless of whether the game is paused or not.
    */
    public void checkSystemInput() {
        if (pause.isPressed()) {
            setPaused(!isPaused());
        }
        if (exit.isPressed()) {
            stop();
        }
        if (newgame.isPressed()) 
        {
        	
        	for (int i=0; i<8;i++)
          { 
        	  winners[i]="";
          }
        	
          this.run();	
        }
        
        
    }
    /**
        Checks input from GameActions that can be pressed
        only when the game is not paused.
    */
    public void checkGameInput() {
        float velocityX = 0;
        float velocityX0 = 0;
        
        
        if (!paused) 
        {
        
	        if (moveLeft.isPressed()) 
	        {
	            velocityX-=Player.SPEED;
	            
	        }
	        if (moveright.isPressed())
	        {
	            velocityX+=Player.SPEED;
	        }
	        
	        
	        if (jump.isPressed() )
	        {
	            player.jump();
	        }
	        
	        //player2
	        if (moveLeft0.isPressed()) 
	        {
	            velocityX0-=Player.SPEED;
	            
	        }
	        if (moveright0.isPressed())
	        {
	            velocityX0+=Player.SPEED;
	        }
	        
	        
	        if (jump0.isPressed() )
	        {
	            player0.jump();
	        }
	        
	        player.setVelocityX(velocityX);
	        player0.setVelocityX(velocityX0);
	
	        //Set Random Velocity
	        player.setVelocityX((float)Math.random()- 0.3f);
	        player0.setVelocityX((float)Math.random()- 0.3f);
	        player1.setVelocityX((float)Math.random()- 0.3f);
	        player2.setVelocityX((float)Math.random()- 0.3f);
	        player3.setVelocityX((float)Math.random()- 0.3f);
	        player4.setVelocityX((float)Math.random()- 0.3f);
	        player5.setVelocityX((float)Math.random()- 0.3f);
	        player6.setVelocityX((float)Math.random()- 0.3f);
	       
        }

        
    }
    public void draw(Graphics2D g) {
        // draw background
        g.drawImage(bgImage, 0, 0, null);

        //draw players sprites
        g.drawImage(player.getImage(),
            Math.round(player.getX()),
            Math.round(player.getY()),
            null);
        
        g.drawImage(player0.getImage(),
                Math.round(player0.getX()),
                Math.round(player0.getY()),
                null);
        
        g.drawImage(player1.getImage(),
                Math.round(player1.getX()),
                Math.round(player1.getY()),
                null);
        
        g.drawImage(player2.getImage(),
                Math.round(player2.getX()),
                Math.round(player2.getY()),
                null);
        
        g.drawImage(player3.getImage(),
                Math.round(player3.getX()),
                Math.round(player3.getY()),
                null);
        
        g.drawImage(player4.getImage(),
                Math.round(player4.getX()),
                Math.round(player4.getY()),
                null);
        
        g.drawImage(player5.getImage(),
                Math.round(player5.getX()),
                Math.round(player5.getY()),
                null);
        
        g.drawImage(player6.getImage(),
                Math.round(player6.getX()),
                Math.round(player6.getY()),
                null);
        
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g.setColor(Color.white);
        g.drawString("Ready! Set! Go!",450,40);
        
      //print the clock        
        g.drawString("Time",500,75);
        g.drawString(clock,500,101);        
       
                
        g.setFont(new Font("Dialog",0,18));
        
        g.drawRect(840, 30, 390, 120);
        g.drawString("Positions",1010,25);
        g.drawString("1st: " + winners[0],850,60);
        g.drawString("2nd: " + winners[1],850,85);
        g.drawString("3th: " + winners[2],850,110);
        g.drawString("4th: " + winners[3],850,135);
        g.drawString("5th: " + winners[4],1050,60);
        g.drawString("6th: " + winners[5],1050,85);
        g.drawString("7th: " + winners[6],1050,110);
        g.drawString("8th: " + winners[7],1050,135);
        
        if (winners[7]!="")
    	{
    	
        	g.setFont(new Font("Dialog",0,30));
        	g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON); 
        	g.setColor(Color.yellow);
        	
    		if(winners[0].equals(splayer))
    		{
    			g.drawString("You Win! " + winners[0] + " 1st Place!",410,180);
    		}
    		else
    		{
    			g.drawString("You Lose! " + winners[0] + " 1st Place!",410,180);
    		}
    		g.drawString("Press N to start a new game or Esc to exit.",350,330);
    		g.dispose();
        	screen.update();        	
    	}
        
               
        
        
    }
    
    //This function manages the winner and places
    public void win(String play) {
            	 
    	//Check The winner
    	int fwinner=0;
    	
	    	for (int i=0;i<=8;i++)
	    	{
	    		if (winners[i]!="")
	        	{
		    		if (winners[i].substring(0, play.length()).contains(play))
		    			{
		    			fwinner=1;		    			
		    			}
	        	}
	    	}
    	
	    //add the player to the place	    	
    	if (fwinner==0)
    	{
	    	for (int i=0;i<=8;i++)
	    	{
	    		if (winners[i]=="")
	    			{
	    			winners[i]= play + " " + clock;
	    			
	    			if(i==7)
	    			{
	    				pause.tap();
	    				timer.stop();	    				
	    			}
	    			break;
	    			}	    		
	    	}
    	}	
    	
    }
    
    
    /**
        Creates GameActions and maps them to keys.
    */
    public void createGameActions() {
        jump = new GameAction("jump", GameAction.DETECT_INITIAL_PRESS_ONLY);
        exit = new GameAction("exit", GameAction.DETECT_INITIAL_PRESS_ONLY);
        
        
        moveLeft = new GameAction("moveLeft");
        moveright = new GameAction("moveRight");
        pause = new GameAction("pause",GameAction.DETECT_INITIAL_PRESS_ONLY);
        
        
        inputManager.mapToKey(exit, KeyEvent.VK_ESCAPE);
        inputManager.mapToKey(pause, KeyEvent.VK_P);
        
        //MAIN MENU OPTIONS
        newgame = new GameAction("newgame",GameAction.DETECT_INITIAL_PRESS_ONLY);
        sp1 = new GameAction("sp1",GameAction.DETECT_INITIAL_PRESS_ONLY);
        sp2 = new GameAction("sp2",GameAction.DETECT_INITIAL_PRESS_ONLY);
        sp3 = new GameAction("sp3",GameAction.DETECT_INITIAL_PRESS_ONLY);
        sp4 = new GameAction("sp4",GameAction.DETECT_INITIAL_PRESS_ONLY);
        sp5 = new GameAction("sp5",GameAction.DETECT_INITIAL_PRESS_ONLY);
        sp6 = new GameAction("sp6",GameAction.DETECT_INITIAL_PRESS_ONLY);
        sp7 = new GameAction("sp7",GameAction.DETECT_INITIAL_PRESS_ONLY);
        sp8 = new GameAction("sp8",GameAction.DETECT_INITIAL_PRESS_ONLY);
        
        inputManager.mapToKey(newgame, KeyEvent.VK_ENTER);
   
        inputManager.mapToKey(sp1, KeyEvent.VK_1);
        inputManager.mapToKey(sp2, KeyEvent.VK_2);
        inputManager.mapToKey(sp3, KeyEvent.VK_3);
        inputManager.mapToKey(sp4, KeyEvent.VK_4);
        inputManager.mapToKey(sp5, KeyEvent.VK_5);
        inputManager.mapToKey(sp6, KeyEvent.VK_6);
        inputManager.mapToKey(sp7, KeyEvent.VK_7);
        inputManager.mapToKey(sp8, KeyEvent.VK_8);
                
        
        // jump with spacebar
        inputManager.mapToKey(jump, KeyEvent.VK_SPACE);
        
        // move with the arrow keys...
        inputManager.mapToKey(moveLeft, KeyEvent.VK_LEFT);
        inputManager.mapToKey(moveright, KeyEvent.VK_RIGHT);
        
        moveLeft0 = new GameAction("moveLeft0");
        moveright0 = new GameAction("moveRight0");
        jump0 = new GameAction("jump0");
        
        // with A and D to player 2
        inputManager.mapToKey(moveLeft0, KeyEvent.VK_A);
        inputManager.mapToKey(moveright0, KeyEvent.VK_D);
        inputManager.mapToKey(jump0, KeyEvent.VK_J);
        // use these lines to map player movement to the mouse
        //inputManager.mapToMouse(moveLeft,
        //  InputManager.MOUSE_MOVE_LEFT);
        //inputManager.mapToMouse(moveRight,
        //  InputManager.MOUSE_MOVE_RIGHT);
    }
    /**
        Load images and creates the Player sprite.
    */
    private void createSprite() {
        // load images
        bgImage = loadImage("images/background.jpg");
        
        //megaman
        Image mega1 = loadImage("images/mega1.png");
        Image mega2 = loadImage("images/mega2.png");
        Image mega3 = loadImage("images/mega3.png");
        Image mega4 = loadImage("images/mega4.png");

        //mario
        Image mario1 = loadImage("images/mario0.png");
        Image mario2 = loadImage("images/mario1.png");
        Image mario3 = loadImage("images/mario2.png");
        Image mario4 = loadImage("images/mario3.png");
      
        //samus
        Image samus1 = loadImage("images/samus0.png");
        Image samus2 = loadImage("images/samus1.png");
        Image samus3 = loadImage("images/samus2.png");
        Image samus4 = loadImage("images/samus3.png");
        
      //goku
        Image goku1 = loadImage("images/goku1.png");
        Image goku2 = loadImage("images/goku2.png");
        Image goku3 = loadImage("images/goku3.png");
        
        //blinky
        Image blinky1 = loadImage("images/blinky0.png");
        
        
        
        // create animations
        Animation anim = new Animation();
       
        anim.addFrame(mario1, 250);
        anim.addFrame(mario2, 250);
        anim.addFrame(mario3, 250);
        anim.addFrame(mario4, 250);
       
        Animation anim2 = new Animation();
        anim2.addFrame(mega1, 300);
        anim2.addFrame(mega2, 300);
        anim2.addFrame(mega3, 300);
        anim2.addFrame(mega4, 300);
      
        //samus
        Animation anim3 = new Animation();
        anim3.addFrame(samus1, 1);
        anim3.addFrame(samus2, 300);
        anim3.addFrame(samus3, 300);
        anim3.addFrame(samus4, 300);
      
        
        //goku
        Animation anim4 = new Animation();
        anim4.addFrame(goku1, 300);
        anim4.addFrame(goku2, 300);
        anim4.addFrame(goku3, 300);
        
        //blinky
        Animation anim5 = new Animation();
        anim5.addFrame(blinky1, 300);
        //anim4.addFrame(goku2, 300);
        //anim4.addFrame(goku3, 300);
       
        player = new Player(anim);
        player.setFloorY(screen.getHeight() - player.getHeight());
      
        player0 = new Player(anim2);
        player0.setFloorY(screen.getHeight() - player0.getHeight() -100);
        
        player1 = new Player(anim3);
        player1.setFloorY(screen.getHeight() - player1.getHeight() -200);
      
        player2 = new Player(anim4);
        player2.setFloorY(screen.getHeight() - player2.getHeight() -290);
        
        player3 = new Player(anim5);
        player3.setFloorY(screen.getHeight() - player3.getHeight() -380);
      
        player4 = new Player(anim2);
        player4.setFloorY(screen.getHeight() - player4.getHeight() -500);
        
        player5 = new Player(anim);
        player5.setFloorY(screen.getHeight() - player5.getHeight() -590);
      
        player6 = new Player(anim2);
        player6.setFloorY(screen.getHeight() - player6.getHeight() -660);
        
        
    }
  
	public void main()
	{
		
		int x=0;
		Image imgPlayer1,imgPlayer2,imgPlayer3,imgPlayer4,imgPlayer5,imgPlayer6,imgPlayer7,imgPlayer8;
		
		bgMenu = loadImage("images/menu.jpg");
			
		imgPlayer1 = loadImage("images/mario0.png");
		imgPlayer2 = loadImage("images/mega1.png");
		imgPlayer3 = loadImage("images/samus0.png");
		imgPlayer4 = loadImage("images/goku0.png");
		imgPlayer5 = loadImage("images/blinky0.png");
		imgPlayer6 = loadImage("images/mega1.png");
		imgPlayer7 = loadImage("images/mario0.png");
		imgPlayer8 = loadImage("images/mega1.png");
		
		Graphics2D g = screen.getGraphics();
		g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
		while(x==0)
		{
			
			//draw menu and character select
			g.drawImage(bgMenu, 0, 0, null);
			g.setColor(Color.white);
			g.setFont(new Font("Dialog",Font.PLAIN,45));
			g.drawString("8Bits Racers", 430, 100);				
			
			g.setFont(new Font("Dialog",Font.PLAIN,FONT_SIZE));
			
			g.drawString("Character Select", 450, 230);
			
			g.drawImage(imgPlayer1, 200, 320, null);
			g.drawImage(imgPlayer2, 300, 335, null);
			g.drawImage(imgPlayer3, 400, 310, null);
			g.drawImage(imgPlayer4, 500, 335, null);
			g.drawImage(imgPlayer5, 600, 350, null);
			g.drawImage(imgPlayer6, 700, 335, null);
			g.drawImage(imgPlayer7, 800, 320, null);
			g.drawImage(imgPlayer8, 900, 335, null);
			
			g.setColor(Color.black);
			g.drawString("1", 225, 460);
			g.drawString("2", 325, 460);
			g.drawString("3", 425, 460);
			g.drawString("4", 525, 460);
			g.drawString("5", 625, 460);
			g.drawString("6", 725, 460);
			g.drawString("7", 825, 460);
			g.drawString("8", 925, 460);
			
			
			g.drawString("Then press Enter to Start or Esc to Exit", 200, 800);
			
			
			if (newgame.isPressed()) 
	        {
				if (splayer=="" || splayer=="Please select a Character!"){
					
					splayer="Please select a Character!";											
				}
				else
				{
					x=1;
					startTime=System.currentTimeMillis();
					paused=false;
				}
	        }
			
			if (exit.isPressed()) {
				System.exit(0);
	        }
			
			if (sp1.isPressed())
			{
				splayer=pnames[0];
			}
			if (sp2.isPressed())
			{
				splayer=pnames[1];
			}
			if (sp3.isPressed())
			{
				splayer=pnames[2];
			}
			if (sp4.isPressed())
			{
				splayer=pnames[3];
			}
			if (sp5.isPressed())
			{
				splayer=pnames[4];
			}
			if (sp6.isPressed())
			{
				splayer=pnames[5];
			}
			if (sp7.isPressed())
			{
				splayer=pnames[6];
			}
			if (sp8.isPressed())
			{
				splayer=pnames[7];
			}
			
			g.drawString("You Selected:   " + splayer, 170, 595);
			screen.update();
//			x=1;
		}		
		
		g.dispose();		
		screen.update();
		
	}
    
}